package com.powergisol.gis.thread;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.powergisol.gis.dao.TabZkMapper;
import com.powergisol.gis.entity.TabProject;
import com.powergisol.gis.entity.TabZkShuiwei;
import com.powergisol.gis.entity.TabZkTuceng;
import com.powergisol.gis.entity.TabZuankong;
import com.powergisol.gis.service.TabProjectService;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

public class Worker extends Thread {
    private String zipPath;
    private List<TabZuankong> tabZuankongList;
    private CacheManager cacheManager;
    private TabProjectService tabProjectService;
    private TabZkMapper tabZkMapper;
    private List<File> files = new ArrayList<>();
    private static String DIRUUID = UUID.randomUUID().toString().replaceAll("-", "");
    String uuid;

    public Worker(List<TabZuankong> tabZuankongList, CacheManager cacheManager, TabProjectService tabProjectService, TabZkMapper tabZkMapper, String uuid, String dirpath) {
        this.tabProjectService = tabProjectService;
        this.cacheManager = cacheManager;
        this.tabZuankongList = tabZuankongList;
        this.tabZkMapper = tabZkMapper;
        this.uuid = uuid;
        this.zipPath = dirpath;
    }

    @Override
    public void run() {
        for (TabZuankong tabZuankong : tabZuankongList) {
            TabZkTuceng tabZkTuceng = new TabZkTuceng();
            tabZkTuceng.setSettingId(tabZuankong.getSettingId());
            tabZkTuceng.setZKBH(tabZuankong.getZKBH());
            TabProject one = tabProjectService.getOne(tabZuankong.getSettingId());
            List<TabZkTuceng> tcs = tabZkMapper.getTCS(tabZkTuceng);
            if (tabZuankong.getZKLX().contains("　")) {
                tabZuankong.setZKLX(tabZuankong.getZKLX().replace("　", ""));
            }
            BigDecimal sd = new BigDecimal(0);
            for (TabZkTuceng t : tcs) {
                sd = sd.add(t.getTCHD());
                t.setTCBH(dataValidation(t.getTCBH()));
                t.setTCDZSD(dataValidation(t.getTCDZSD()));
                t.setTCDZCY(dataValidation(t.getTCDZCY()));
                t.setTCDCHD(dataValidation(t.getTCDCHD()));
                t.setTCSDCY(dataValidation(t.getTCSDCY()));
                t.setTCLM(dataValidation(t.getTCLM()));
                t.setTCMS(dataValidation(t.getTCMS()));
            }
            TabZkShuiwei tabZkShuiwei = new TabZkShuiwei();
            tabZkShuiwei.setZKBH(tabZuankong.getZKBH());
            tabZkShuiwei.setSettingId(tabZuankong.getSettingId());
            List<TabZkShuiwei> sws = tabZkMapper.getSWS(tabZkShuiwei);
            Resource resource = new ClassPathResource("template/template1.1.xlsx");
            String fileName = null;
            if (one == null || one.getGCMC() == null) {
                fileName = zipPath + File.separator + System.currentTimeMillis() + ".xlsx";
            } else {
                fileName = zipPath + File.separator + one.getGCMC() + "_" + tabZuankong.getZKBH() + ".xlsx";
            }

            File file = new File(fileName);
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            files.add(file);
            ExcelWriter excelWriter = null;
            try {
                excelWriter = EasyExcel.write(fileName).withTemplate(resource.getInputStream()).build();
            } catch (IOException e) {
                e.printStackTrace();
            }
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
            // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1，然后多个list必须用 FillWrapper包裹
            excelWriter.fill(new FillWrapper("TabZkTuceng", tcs), fillConfig, writeSheet);
            excelWriter.fill(new FillWrapper("TabZkShuiwei", sws), fillConfig, writeSheet);
            Map<String, Object> map = new HashMap<>();
            map.put("ZKBH", tabZuankong.getZKBH());
            map.put("ZKX", null == tabZuankong.getZKOX()?tabZuankong.getZKX():tabZuankong.getZKOX());
            map.put("ZKY", null == tabZuankong.getZKOY()?tabZuankong.getZKY():tabZuankong.getZKOY());
            map.put("ZKBG", tabZuankong.getZKBG());
            map.put("ZKZJ", tabZuankong.getZKZJ());
            if (tabZuankong.getZKSD() == null || tabZuankong.getZKSD() == new BigDecimal(0)) {
                map.put("ZKSD", sd);
            } else {
                map.put("ZKSD", tabZuankong.getZKSD());
            }
            map.put("ZKLX", dataValidation(tabZuankong.getZKLX()));
            map.put("ZKPZTSDXS", tabZuankong.getZKPZTSDXS());
            map.put("tuceng", tcs.size());
            map.put("shuiwei", sws.size());
            map.put("GCBH", dataValidation(one.getGCBH()));
            map.put("ZKKSRQ", dataValidation(tabZuankong.getZKKSRQ()));
            map.put("ZKZZRQ", dataValidation(tabZuankong.getZKZZRQ()));
            map.put("Hcdm01", tabZuankong.getHcdm01());
            map.put("Hcdm02", tabZuankong.getHcdm02());
            map.put("Hcdm05", tabZuankong.getHcdm05());
            excelWriter.fill(map, writeSheet);
            // 别忘记关闭流
            excelWriter.finish();
        }

        Cache cache = cacheManager.getCache("cache-data");
        cache.put(uuid, files.size());
    }

    private String dataValidation(String str){
        if(str == null){
            return null;
        }else if(str.trim().equals("null")){
            return null;
        }else{
            return str.trim();
        }
    }
}
